const { BrowserWindow } = require('electron');
const path = require('path');

let loginWindow;
let mainWindow;
let settingsWindow;

function createLoginWindow() {
    loginWindow = new BrowserWindow({
        width: 400,
        height: 300,
        webPreferences: {
            preload: path.join(__dirname, 'preload.js'),
            nodeIntegration: false,
            contextIsolation: true,
        },
    });
    loginWindow.setMenu(null);
    loginWindow.loadFile('index.html');
}

function createMainWindow() {
    mainWindow = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
            preload: path.join(__dirname, 'preload.js'),
            nodeIntegration: false,
            contextIsolation: true,
        },
    });
    mainWindow.setMenu(null);
    mainWindow.loadFile('main.html');
}

function createSettingsWindow() {
    if (!settingsWindow) {
        settingsWindow = new BrowserWindow({
            width: 800,
            height: 600,
            webPreferences: {
                nodeIntegration: false,
                contextIsolation: true,
                preload: path.join(__dirname, 'preload.js')
            }
        });
        settingsWindow.setMenu(null);
        settingsWindow.loadFile('server_setting.html');

        settingsWindow.on('closed', () => {
            settingsWindow = null;
        });
    }
}

function closeLoginWindow() {
    if (loginWindow) loginWindow.close();
}

module.exports = { createLoginWindow, createMainWindow, closeLoginWindow, createSettingsWindow };